package com.brsanthu.sgwidgets.client;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.DateChooser;
import com.smartgwt.client.widgets.Dialog;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Slider;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.ValueChangedEvent;
import com.smartgwt.client.widgets.events.ValueChangedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

@SuppressWarnings("synthetic-access")
public class DateTimeChooser extends Dialog {

    private Label ampmLabel = null;
    private Slider hourSlider;
    private Slider minuteSlider;
    private Slider secondSlider;
    private Label timeLabel;
    private DateChooser dateChooser;
    private CallbackHandler handler;
    private static DateTimeFormat dfDateTime = DateTimeFormat.getFormat("yyyy/MM/dd h:m:s a");
    private static DateTimeFormat dfDate = DateTimeFormat.getFormat("yyyy/MM/dd");
    private static DateTimeFormat dfHour = DateTimeFormat.getFormat("h");
    private static DateTimeFormat dfMinute = DateTimeFormat.getFormat("m");
    private static DateTimeFormat dfSecond = DateTimeFormat.getFormat("s");
    private static DateTimeFormat dfAmpm = DateTimeFormat.getFormat("a");

    public DateTimeChooser() {
        initChooser();
    }

    private void initChooser() {

        setAutoCenter(true);
        setIsModal(true);
        setShowHeader(true);
        setShowEdges(true);
        setMargin(0);
        setPadding(0);
        setEdgeSize(10);
        setWidth(300);
        setHeight(260);
        setDismissOnEscape(true);
        setDismissOnOutsideClick(true);
        setShowToolbar(false);
        setTitle("Select Date and Time");

        Map bodyDefaults = new HashMap();
        bodyDefaults.put("layoutLeftMargin", 5);
        setBodyDefaults(bodyDefaults);

        HLayout layout = new HLayout();
        layout.setMembersMargin(5);
        layout.setWidth(200);
        dateChooser = new DateChooser();
        dateChooser.setShowEdges(false);
        dateChooser.setData(new Date());
        layout.addMember(dateChooser);

        VLayout vlayout2 = new VLayout();
        HLayout hlayout2 = new HLayout(5);

        timeLabel = ClientUtils.createLabel("12:59");
        timeLabel.setWidth("60");
        timeLabel.setAlign(Alignment.RIGHT);

        ValueChangedHandler valueChangeHandler = new ValueChangedHandler() {
            @Override
            public void onValueChanged(ValueChangedEvent event) {
                timeLabel.setContents(((int)hourSlider.getValue()) + ":" + ((int)minuteSlider.getValue()) +  ":" + ((int)secondSlider.getValue()));
                timeLabel.markForRedraw();
            }
        };

        hourSlider = getSlider("Hour", 1, 12);
        hourSlider.addValueChangedHandler(valueChangeHandler);

        minuteSlider = getSlider("Minute", 0, 59);
        minuteSlider.addValueChangedHandler(valueChangeHandler);

        secondSlider = getSlider("Second", 0, 59);
        secondSlider.addValueChangedHandler(valueChangeHandler);

        hlayout2.addMember(hourSlider);
        hlayout2.addMember(minuteSlider);
        hlayout2.addMember(secondSlider);

        vlayout2.addMember(hlayout2);

        HLayout layout3 = new HLayout();
        layout3.addMember(timeLabel);

        ampmLabel = ClientUtils.createLabel("AM");
        ampmLabel.setCanSelectText(false);
        ampmLabel.setPadding(1);
        ampmLabel.setAlign(Alignment.CENTER);
        ampmLabel.setWidth(30);
        ampmLabel.setStyleName("normal, sgwidgetsAmPm");
        ampmLabel.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                if ("AM".equalsIgnoreCase(ampmLabel.getContents())) {
                    ampmLabel.setContents("PM");
                } else {
                    ampmLabel.setContents("AM");
                }
            }
        });
        layout3.addMember(ampmLabel);

        vlayout2.addMember(layout3);
        layout.addMember(vlayout2);

        addItem(layout);

        HLayout layout4 = new HLayout();
        layout4.setAlign(Alignment.CENTER);

        Button button = new Button("Set");
        button.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                if (handler != null) {
                    handler.handle(getData());
                }
                hide();
            }
        });

        layout4.addMember(button);

        button = new Button("Cancel");
        button.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                hide();
            }
        });

        layout4.addMember(button);

        addItem(ClientUtils.createSpacer(3));
        addItem(layout4);
    }

    private Slider getSlider(String title, int min, int max) {
        Slider slider = new Slider(title);
        slider.setLabelSpacing(5);
        slider.setLabelWidth(10);
        slider.setHeight(150);
        slider.setWidth(10);
        slider.setMinValue(min);
        slider.setMaxValue(max);
        slider.setShowRange(false);
        slider.setMaxValueLabel("");
        return slider;
    }

    public void setData(Date date) {
        dateChooser.setData(date);
        hourSlider.setValue(Integer.parseInt(dfHour.format(date)));
        minuteSlider.setValue(Integer.parseInt(dfMinute.format(date)));
        secondSlider.setValue(Integer.parseInt(dfSecond.format(date)));
        ampmLabel.setContents(dfAmpm.format(date));
    }

    public Date getData() {
        String dateString = dfDate.format(dateChooser.getData()) + " " + ((int) hourSlider.getValue())
            + ":" + ((int) minuteSlider.getValue()) + ":" + ((int) secondSlider.getValue()) + " " + ampmLabel.getContents();
        return dfDateTime.parse(dateString);
    }

    public void addCallbackHandler(CallbackHandler handler) {
        this.handler = handler;
    }
}
